# -*- encoding:utf-8 -*-
# @Time: 2022/11/23 23:19
# @Author: qiujixian
# @Email: 1253598817@qq.com
# @File: serializers.py
# @Software: PyCharm

from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer
from .models import *
from users.serializers import UserSerializer

class ClassesSerializer(ModelSerializer):
    memberDetail = SerializerMethodField()

    class Meta:
        model = Classes
        exclude = ['is_delete']

    def get_memberDetail(self, instance):
        # 分类
        members = UserSerializer(instance.member.all(), many=True).data  # 获取当前班级所有的用户数据并序列化
        return {
            'teachers': [i['username'] for i in members if i['is_superuser'] or 1 in i['groups']],
            'students': [i['username'] for i in members if 2 in i['groups']],
        }

